﻿var express = require("express");
var app = express();
var session = require("express-session");
var bodyParser = require('body-parser')
var db = require('./db.js')
const crypto = require('crypto');
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

app.use(session({
    secret: '1',
    resave: true,
    saveUninitialized: true
}));


//注册
app.post("/users/registr", function (req, res) {
    var md5 = crypto.createHash('md5');
    req.body.Pw = md5.update(req.body.Pw).digest('base64')
    // req.body.Pw = md5.update(req.body['Pw']).digest('base64');  //加密后的值

    db.insertDocuments('user',req.body, function (err, result) {
        if (err) {
            res.send(err)
        }
        else {
            console.log(req.body);
            req.session.login = "1";//1代表这个人是注册成功的
            req.session.username = req.body.user;
            req.session.sid = req.body.id;

            res.json({"result": 1});
        }
    })


    // console.log(sql);
});
//登录
app.post("/users/login", function (req, res) {

    db.findDocuments('user', {id: req.body.id}, function (err, result) {
        if (err)
            console.log(err);
        var md5 = crypto.createHash('md5');
        var originPw = md5.update(req.body.Pw).digest('base64')
        if (result.length == 0) {
            res.json({"result": 0});
            return
        }
        if (result[0].Pw == originPw) {
            var user = result[0].user
            var id = result[0].id
            req.session.username = user;
            req.session.sid = id;
            req.session.login = "1";

            res.json({"result": 1,'username':user,'sid':id});

        } else {
            res.json({"result": 2});
        }
    })
});

app.post("/users/user", sess);
function sess(req, res, next) {
    if (req.session.login == "1") {
        //console.log('进来了显示页面');
        var username = req.session.username;
        var login = req.session.login;
        var sid = req.session.sid;
        var book_answer = req.session.book_answer;
        var book_bt = req.session.book_bt;
        var book_name = req.session.book_name;
        var fx_title = req.session.fx_title;
        var fx_neirong = req.session.fx_neirong;
        var fx_value = req.session.fx_value;
        res.send({"result": 1,'username': username, 'sid': sid, 'book_answer':book_answer, 'book_bt':book_bt, 'book_name':book_name, 'fx_title':fx_title, 'fx_neirong':fx_neirong, 'fx_value':fx_value});
    } else {
        res.json({"result": 0});
    }
};
//退出登录
app.post("/users/logout",function (req,res) {
    req.session.login = "0";
    res.json({"result": 1});
    //console.log(req.session);


});

//提交问题
app.post("/users/tiwen", function (req, res) {
    db.insertDocuments('question',req.body, function (err, result) {
        if (err) {
            res.send(err)
        }
        else {
            console.log(req.body);
            req.session.book_name = req.body.book_name;//1代表这个人是注册成功的
            req.session.username = req.body.username;
            req.session.book_bt = req.body.book_bt;
            req.session.book_answer = req.body.book_answer;
            res.json({"result": 1});
        }
    })

});

//提交分享
app.post("/users/share", function (req, res) {
    db.insertDocuments('share',req.body, function (err, result) {
        if (err) {
            res.send(err)
        }
        else {
            console.log(req.body);
            req.session.fx_title = req.body.fx_title;
            req.session.username = req.body.username;
            req.session.fx_value = req.body.fx_value;
            req.session.fx_neirong = req.body.fx_neirong;
            res.json({"result": 1});
        }
    })

});

//话题关注

app.post("/users/guanzhu",function(req,res) {
    db.insertDocuments('guanzhu',req.body,function(err,result){
        if(err) {
            res.send(err)
        }else {
            console.log(req.body);
            req.session.guanzhu = "1";//表示回答的数据传递成功
            req.session.xk = req.body.xk;
            req.session.gz = req.body.gz;
            req.session.username = req.body.username;

            res.json({"result":1 });
        }
    })
});

//用户关注2

app.post("/users/guanzhu2",function(req,res) {
    db.insertDocuments('guanzhu2',req.body,function(err,result){
        if(err) {
            res.send(err)
        }else {
            console.log(req.body);
            req.session.guanzhu = "1";//表示回答的数据传递成功
            req.session.yhm = req.body.yhm;
            req.session.username = req.body.user;

            res.json({"result":1 });
        }
    })
});

//点赞



//提交回答

app.post("/users/answer",function(req,res) {
    db.insertDocuments('answer',req.body,function(err,result){
        if(err) {
            res.send(err)
        }else {
            console.log(req.body);
            req.session.answer = "1";//表示回答的数据传递成功
            req.session.value = req.body.title;
            req.session.username = req.body.username;
            res.json({"result":1 });
        }
    })
});



/*
app.post("/users/answer", sess);
function sess(req, res, next) {
    if (req.session.answer == "1") {
        //console.log('进来了显示页面');
        var username = req.session.username;
        var answer = req.session.answer;
        var sid = req.session.sid;
        res.send({"result": 1,'username': username, 'sid': sid});
    } else {
        res.json({"result": 0});
    }
};
*/
//搜索功能

app.post("/users/search", function (req, res) {

    db.findDocuments('answer', {title: req.body.title}, function (err, result) {
        if (err)
            console.log(err);
    //    var md5 = crypto.createHash('md5');
        var originTitle = req.body.title;
        if (result.length == 0) {
            res.json({"result": 0});
            return
        }
        if (result[0].title == title) {
            var user = result[0].user
            var title = result[0].title
            req.session.username = user;
            req.session.title = title;
            req.session.answer = "1";

            res.json({"result": 1,'user':user,'sid':id});

        } else {
            res.json({"result": 2});
        }
    })
});



app.listen(3000);